<?php


namespace app\mobile\controller;


use think\Controller;

class Login extends Controller
{
    public function login(){
        if($this->request->isAjax()){
            $param = $this->request->param();
            $mobile = $param['account'];
            $password = md5($param['password']);
            $member = db("member")->where("mobile",$mobile)->find();
            if(empty($member)) return ['state' => -1,'msg' => '账号或者密码错误'];
            if($member['password'] !=$password) return ['state' => -1,'msg' => '账号或者密码错误'];
            session('member_id',$member['id']);
            return ['state' => 200,'msg' => 'SUCCESS'];
        }
        return view('login');
    }
    public function register()
    {
        return view('register');
    }
    public function executeRegister()
    {
        $param = $this->request->param();
        $mobile = $param['mobile'];
        $psw = $param['password'];
        $psw_ag = $param['re_password'];
        if(empty($mobile) || empty($psw)) return ['state' => -1,'msg' => '账号或密码不能为空'];
        $member = db("member")->where("mobile",$mobile)->find();
        if(!empty($member)) return ['state' => -1,'msg' => '该账号已存在'];
        if($psw != $psw_ag) return ['state' => -1,'msg' => '两次密码不一致'];
        $nickname = 'cx'.rand(9999,100000);
        $res = db('member')->insert(['mobile' => $mobile,'password' => md5($psw),'nickname' => $nickname,'money' => 10]);
        if(!$res) return ['state' => -1,'msg' => 'ERROR'];
        return ['state' => 200,'msg' => 'SUCCESS'];
    }
}